﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace tu_xls_3
{
    class Program
    {
        static void Main(string[] args)
        {
            Excel.Application xlsApp = new Excel.ApplicationClass();
            Excel.Workbook xlsWorkBook;
            Excel.Worksheet xlsWorkSheet;
            Excel.Range chartRange;
            object misVal = System.Reflection.Missing.Value;

            xlsWorkBook = xlsApp.Workbooks.Add(misVal);
            xlsWorkSheet = (Excel.Worksheet)xlsWorkBook.Worksheets.get_Item(1);

            // add data
            xlsWorkSheet.Cells[4, 2] = "";
            xlsWorkSheet.Cells[4, 3] = "Student1";
            xlsWorkSheet.Cells[4, 4] = "Student2";
            xlsWorkSheet.Cells[4, 5] = "Student3";

            xlsWorkSheet.Cells[5, 2] = "Term1";
            xlsWorkSheet.Cells[5, 3] = "80";
            xlsWorkSheet.Cells[5, 4] = "65";
            xlsWorkSheet.Cells[5, 5] = "45";

            xlsWorkSheet.Cells[6, 2] = "Term2";
            xlsWorkSheet.Cells[6, 3] = "78";
            xlsWorkSheet.Cells[6, 4] = "67";
            xlsWorkSheet.Cells[6, 5] = "89";

            xlsWorkSheet.Cells[7,2] = "Term3";
            xlsWorkSheet.Cells[7,3] = "88";
            xlsWorkSheet.Cells[7,4] = "77";
            xlsWorkSheet.Cells[7,5] = "78";

            xlsWorkSheet.Cells[8,2] = "Term4";
            xlsWorkSheet.Cells[8,3] = "98";
            xlsWorkSheet.Cells[8,4] = "60";
            xlsWorkSheet.Cells[8,5] = "78";

            xlsWorkSheet.Cells[9, 2] = "Total";
            xlsWorkSheet.Cells[9, 3] = "512";
            xlsWorkSheet.Cells[9, 4] = "244";
            xlsWorkSheet.Cells[9, 5] = "455";

            //see note: tu_xls_3.doc
            xlsWorkSheet.get_Range("b2", "e3").Merge(false);
           
            //see note
            chartRange = xlsWorkSheet.get_Range("b2", "e3");
            chartRange.FormulaR1C1 = "MARK LIST";
            chartRange.HorizontalAlignment = 3;
            chartRange.VerticalAlignment = 3;

            //see note
            chartRange = xlsWorkSheet.get_Range("b4", "e4");
            chartRange.Font.Bold = true;
            chartRange = xlsWorkSheet.get_Range("b9", "e9");
            chartRange.Font.Bold = true;

            //see note
            chartRange = xlsWorkSheet.get_Range("b2", "e9");
            chartRange.BorderAround(
                    Excel.XlLineStyle.xlContinuous,
                    Excel.XlBorderWeight.xlMedium,
                    Excel.XlColorIndex.xlColorIndexAutomatic,
                    Excel.XlColorIndex.xlColorIndexAutomatic
                );

            //Save
            string xls_path = "c:\\sample.xls";
            xlsWorkBook.SaveAs(
                    xls_path,
                    Excel.XlFileFormat.xlWorkbookNormal,
                    misVal,
                    misVal,
                    misVal,misVal,
                    Excel.XlSaveAsAccessMode.xlExclusive,
                    misVal, misVal,misVal,misVal,misVal
                );
            xlsWorkBook.Close(true, xls_path, misVal);
            xlsApp.Quit();

            Console.ReadLine();
        }
    }
}
